import 'package:flutter/material.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../presenter/state_search_presenter.dart';
import '../../common/novel/novel_list_tile.dart';

class SearchPage extends StatefulWidget {
  const SearchPage(
      {Key? key, required this.presenter, required this.controller})
      : super(key: key);
  static const routeName = '/search';

  final StateSearchPresenter presenter;
  final SearchController controller;
  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  late TextEditingController _textController;

  @override
  void initState() {
    _textController = TextEditingController();
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    widget.controller.searchByText("");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(I18n.search),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextField(
            onChanged: (value) {
              setState(() {});
            },
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.search),
            ),
            controller: _textController,
            onSubmitted: widget.controller.searchByText,
          ),
          const Padding(
            padding: EdgeInsets.all(8.0),
            child: Text(I18n.hot_search_history),
          ),
          Expanded(
            flex: 1,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: [
                TextButton(
                  onPressed: (() {
                    widget.controller.clearSearchHistory();
                    _textController.clear();
                    widget.controller.searchByText('');
                  }),
                  child: const Text(I18n.clear),
                ),
                ...widget.presenter.searchList
                    .where((element) => element.content.isNotEmpty)
                    .map(
                      (search) => TextButton(
                        onPressed: (() {
                          _textController.text = search.content;
                          widget.controller.searchByText(_textController.text);
                        }),
                        child: Text(search.content),
                      ),
                    ),
              ],
            ),
          ),
          const Padding(
            padding: EdgeInsets.all(8.0),
            child: Text(I18n.hot_novel),
          ),
          Expanded(
            flex: 9,
            child: ListView(
              children: [
                ...widget.presenter.searchNovelList.map(
                  (searchNovel) => NovelListTile(
                    novel: searchNovel,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
